<template>
    <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="110px" :disabled="disableEditSubmit">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="所属项目" prop="projectname" :rules="rules.validateRequire">
                        <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector" clearable style="width:100%">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目编号" prop="projectcode">
                        <el-input v-model="model.projectcode" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="财务期间月份" prop="financialmonth" :rules="rules.validateRequire">
                        <el-date-picker v-model="model.financialmonth" type="month" placeholder="财务期间月份" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="月成本" prop="cost" :rules="rules.validateRequire">
                        <el-input-number v-model="model.cost" controls-position="right" :step="1" :min="0" :precision="2" style="width:93%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-row>
                <el-col :span="12">
                    <el-form-item label="月成本-工资" prop="costwages">
                        <el-input-number v-model="model.costwages" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="月成本-差旅" prop="costtravel">
                        <el-input-number v-model="model.costtravel" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="月成本费用" prop="costexpenses">
                        <el-input-number v-model="model.costexpenses" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="月成本车辆" prop="costcars">
                        <el-input-number v-model="model.costcars" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="月成本其他" prop="costothers">
                        <el-input-number v-model="model.costothers" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-row>
                <el-col :span="24">
                    <el-form-item label="成本说明" prop="costexplain">
                        <el-input v-model="model.costexplain" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="确认收入" prop="confirmincome" :rules="rules.validateRequire">
                        <el-input-number v-model="model.confirmincome" controls-position="right" :step="1" :min="0" :precision="2" style="width:93%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="确认" prop="checked">
                        <el-radio-group v-model="model.checked" disabled>
                            <el-radio :label="parseInt(item.code)" v-for="item in codeModel.checkedList" :key="item.code">{{ item.name }}</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="收入备注" prop="incomeexplain">
                        <el-input v-model="model.incomeexplain" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="经办公司" prop="orgname">
                        <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="经办部门" prop="deptname" :required="true" :rules="rules.validateRequire">
                        <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectDept" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="经办人工号" prop="usercode">
                        <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="经办人工号" prop="usercode" :required="true" :rules="rules.validateRequire">
                        <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="description">
                        <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
            <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">
                <svg-icon icon-class="save" />保存</el-button>
        </div>
        <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
/** 子组件 */
import tlpmProjectRef from "@/views/ProjectManage/pmProject/refList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
    mixins: [mixin_edit],
    components: { tlpmProjectRef },
    data() {
        return {
            moduleTitle: "项目财务成本与确认收入",
            urlPrefix: "/pm/costIncome/",
            url: {
                // 额外的url
                queryOrgByDeptId: "/pm/resourceArchives/queryOrgByDeptId"
            }
        }
    },
    props: {
        projectId: {
            type: [Number, String],
            default: function() {
                return ""
            }
        },
        editFlag: {
            type: Boolean,
            default: true
        }
    },
    methods: {
        loaded() {
            // 页面加载完毕执行
            if (this.isAdd) {
                this.$set(this.model, "checked", 0)
                this.$set(this.model, "orgcode", this.org.code)
                this.$set(this.model, "orgname", this.org.name)
                this.$set(this.model, "deptcode", this.dept.code)
                this.$set(this.model, "deptname", this.dept.fullname)
                this.$set(this.model, "usercode", this.user.code)
                this.$set(this.model, "username", this.user.name)
            }
        },
        /** 打开项目参照 */
        openpmProjectRefDialog() {
            this.$refs.pmProjectRef.visible = true
        },
        /** 获取项目参数数据 */
        getpmProjectRefData(selectionRow, selectKey, selectionValue) {
            const { id, code, name, planstarttime, planendtime, actualstarttime, actualendtime } = selectionRow
            this.$set(this.model, "projectid", id)
            this.$set(this.model, "dispprojectid", name)
            this.$set(this.model, "projectcode", code)
            this.$set(this.model, "projectname", name)
            this.$set(this.model, "planstarttime", planstarttime)
            this.$set(this.model, "planendtime", planendtime)
            this.$set(this.model, "actualstarttime", actualstarttime)
            this.$set(this.model, "actualendtime", actualendtime)
        },
        onSelectDept(selectedDepts) {
            const selectedDept = selectedDepts[0]
            const param = {
                deptId: selectedDept.id
            }
            getAction(this.url.queryOrgByDeptId, param).then((res) => {
                if (res.success) {
                    const { code: orgCode, name: orgName } = res.result
                    this.$set(this.model, "orgcode", orgCode)
                    this.$set(this.model, "orgname", orgName)
                } else {
                    this.$tap.alert("已选部门关联公司信息错误，请联系系统管理员")
                }
            })
        }
        /** 提交前执行   暂时取消该限制*/
        // beforeSubmit() {
        //     const { planstarttime, planendtime, actualstarttime, actualendtime } = this.model
        //     let min_boundary = planstarttime
        //     if (actualstarttime && actualstarttime < planstarttime) {
        //         min_boundary = actualstarttime
        //     }
        //     let max_boundary = planendtime
        //     if (actualendtime && actualendtime > planendtime) {
        //         max_boundary = actualendtime
        //     }
        //     const financialDate = new Date(this.model.financialmonth)
        //     const min_date = new Date(min_boundary)
        //     const max_date = new Date(max_boundary)
        //     min_date.setDate(1)
        //     max_date.setDate(1)
        //     if (financialDate < min_date || financialDate > max_date) {
        //         this.$tap.alert("财务期间月份，应该在项目的开工时间和完工时间以内")
        //         return false
        //     }
        //     return true
        // }
    }
}
</script>
